# Задание A

n, m = map(int, input().split())

price = [list(input().split()) for i in range(n)]
seats = [list(input().split()) for i in range(n)]
income = []

for i in range(len(seats)):
    for j in range(len(seats[i])):
        if seats[i][j] == "1":
            income.append(price[i][j])
print(sum(map(int, income)))
# Задание B

n = int(input())
soldiers = [list(input().split()) for i in range(n)]
defence_rating = []

for i in range(len(soldiers)):
    for j in range(len(soldiers[i])):
        if i == 0 or i == len(soldiers) - 1:
            defence_rating.append(soldiers[i][j])
        elif i != 0 and i != len(soldiers) - 1 and (j == 0 or j == len(soldiers) - 1):
            defence_rating.append(soldiers[i][j])

print(sum(map(int, defence_rating)))

# Задание C

n = int(input())
List = [list(input().split()) for i in range(n)]
flag = "no"
for i in range(n):
    for j in range(n):
        if List[i][j] == List[j][i]:
            flag = "yes"
print(flag)

# Задание D

a, b = map(int, input().split())
c, d = map(int, input().split())

print(c+1, d)
print(c, d+1)
print(c-1, d)
print(c, d-1)
